package xyz.jpcr.covid19app.Activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import xyz.jpcr.covid19app.Adapter.CityAdapter;
import xyz.jpcr.covid19app.Adapter.CountryAdapter;
import xyz.jpcr.covid19app.Api.OverallApiClient;
import xyz.jpcr.covid19app.Model.Country.Cities;
import xyz.jpcr.covid19app.Model.Country.CountryResults;
import xyz.jpcr.covid19app.Model.WorldWiseResults.WorldWiseResults;
import xyz.jpcr.covid19app.R;
import xyz.jpcr.covid19app.Util.ILocationUtil;
import xyz.jpcr.covid19app.Util.IStoreUtil;

public class CountryWiseActivity extends AppCompatActivity {
    IStoreUtil iStoreUtil = IStoreUtil.getInstance();
    ILocationUtil iLocationUtil = ILocationUtil.getInstance(this);
    Toolbar toolbar;
    RecyclerView recyclerView;
    SwipeRefreshLayout country_swipeRefreshLayout;
    EditText search;
    CountryAdapter countryAdapter;
    ArrayList<WorldWiseResults> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_country_wise_data);

        toolbar = findViewById(R.id.layoutToolBar);
        recyclerView = findViewById(R.id.countrywise_recyclerview);
        country_swipeRefreshLayout = findViewById(R.id.country_swipefreshLayout);
        search = findViewById(R.id.country_search_editText);
        country_swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getJson();
            }
        });
        country_swipeRefreshLayout.setRefreshing(true);
        getJson();


        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        toolbar.setTitle("World");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                filter(s.toString());
            }
        });

    }



    private void filter(String text) {
        ArrayList<WorldWiseResults> filteredList = new ArrayList<>();
        for(WorldWiseResults worldWiseResults : list) {
            if(worldWiseResults.getCountry().toLowerCase().contains(text.toLowerCase())) {
                filteredList.add(worldWiseResults);
            }
        }
        countryAdapter.filterList(filteredList);
    }

    private void getJson() {
        Call<ArrayList<WorldWiseResults>> call = OverallApiClient.getInstance().getApi().getWorldWiseData();
        call.enqueue(new Callback<ArrayList<WorldWiseResults>>() {
            @Override
            public void onResponse(Call<ArrayList<WorldWiseResults>> call, Response<ArrayList<WorldWiseResults>> response) {
                if(response.code() == 200 && response.body() != null) {
                    list = response.body();
                    Collections.sort(list, new Comparator<WorldWiseResults>() {
                        @Override
                        public int compare(WorldWiseResults o1, WorldWiseResults o2) {
                            if(o1.getCases() > o2.getCases())
                                return -1;
                            else
                                return 1;
                        }
                    });
                    countryAdapter = new CountryAdapter(CountryWiseActivity.this, list);
                    recyclerView.setAdapter(countryAdapter);
                }
                else {
                    Toast.makeText(CountryWiseActivity.this, CountryWiseActivity.this.getText(R.string.network_slow), Toast.LENGTH_SHORT).show();
                }
                country_swipeRefreshLayout.setRefreshing(false);
            }

            @Override
            public void onFailure(Call<ArrayList<WorldWiseResults>> call, Throwable t) {
                country_swipeRefreshLayout.setRefreshing(false);
                Log.e(CountryWiseActivity.this.toString(), "onFailure: " + t.getCause());
                Toast.makeText(CountryWiseActivity.this, CountryWiseActivity.this.getText(R.string.network_slow), Toast.LENGTH_SHORT).show();
            }
        });
    }

}


